/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.fanli.ui;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.util.ImageUtils;
import com.mol.market.common.widget.IndicatorDrawable;
import com.mol.market.common.widget.SlowGallery;

/**
 * 没有小窗口的TopGallery
 * @author Chaos
 * @date 2012-07-25
 */
public class FanliTopGallery implements OnItemClickListener,OnItemSelectedListener {

    private Activity mContext;
    private RelativeLayout mHomeGallery;
    private ArrayList<HashMap<String, Object>> mDataSource;
    private SlowGallery mGallery;
    private IndicatorDrawable mIndicator;
    private int mCurrentPage;
    private TextView mTitle;

    public FanliTopGallery(Activity ctx, ViewGroup parent, ArrayList<HashMap<String, Object>> data) {
        mContext = ctx;
        mDataSource = data;
        mHomeGallery = (RelativeLayout) LayoutInflater.from(ctx).inflate(
                R.layout.home_gallery_layout, parent, false);
        mHomeGallery.setFocusableInTouchMode(true);

        mGallery = (SlowGallery) mHomeGallery.findViewById(R.id.home_gallery);
        mTitle = (TextView) mHomeGallery.findViewById(R.id.home_gallery_title);
        ImageView icon = 
          (ImageView) mHomeGallery.findViewById(R.id.home_gallery_user);
        icon.setVisibility(View.GONE);
        mGallery.setOnItemSelectedListener(this);
        mGallery.setOnItemClickListener(this);
        mIndicator = (IndicatorDrawable) mHomeGallery.findViewById(R.id.home_incidator);
        mIndicator.setPageNumber(data.size());
        initImage();
    }

    public View getView() {
        return mHomeGallery;
    }

    private void initImage() {
      TopRecommendAdapter adapter = new TopRecommendAdapter(mContext, mDataSource);
      mGallery.setAdapter(adapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
      mIndicator.setPage(arg2);
      mCurrentPage = arg2;
      updateTitle();
    }
    
    private void updateTitle() {

      HashMap<String, Object> item = mDataSource.get(mCurrentPage);
      String title = (String) item.get(Constants.KEY_PRODUCT_NAME);
      mTitle.setText(title);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        
        HashMap<String, Object> item = mDataSource.get(arg2);

        String id = (String)item.get(Constants.KEY_ID);
        Intent detailIntent = new Intent(mContext,
            FanliPreloadActivity.class);
        detailIntent.putExtra(Constants.EXTRA_PRODUCT_ID, id);
        mContext.startActivity(detailIntent);
    }
    
    
    /**
     * 由于原有的TopRecommendAdapter是static的,使得数据不能私有,所以这里复制一份私有的Adapter
     * @author Chaos	
     *
     */
   private class TopRecommendAdapter extends BaseAdapter {

        private Context mContext;
        private ArrayList<HashMap<String, Object>> mDataSource;
        private LayoutInflater mLayoutInflater;

        public TopRecommendAdapter(Context context, ArrayList<HashMap<String, Object>> data) {
            mContext = context;
            mDataSource = data;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            if (mDataSource == null || mDataSource.size() == 0) {
                return 0;
            }
            return mDataSource.size();
        }

        @Override
        public Object getItem(int position) {
            return mDataSource.get(position);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView v = null;
            if (convertView == null) {
                v = (ImageView) mLayoutInflater.inflate(R.layout.home_gallery_item, parent, false);
            } else {
                v = (ImageView) convertView;
            }
            Drawable old = v.getDrawable();
            v.setImageDrawable(null);
            if (old != null) {
                old.setCallback(null);
            }
            
            Object picUri = mDataSource.get(position).get(Constants.KEY_BANNER);
            if (picUri instanceof String) {
                ImageUtils.downloadHomeTopDrawable(mContext, (String) picUri, v);
            } else if (picUri instanceof Integer) {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inPurgeable = true;
                o.inInputShareable = true;
                Bitmap bmp = BitmapFactory
                        .decodeResource(mContext.getResources(), (Integer) picUri, o);
                Drawable d = ImageUtils.getMaskDrawable(mContext);
                v.setImageDrawable(d);
                v.setBackgroundDrawable(new BitmapDrawable(bmp));
            }
            
            return v;
        }
    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {
      // Do nothing
    }

}
